import requests
from bs4 import BeautifulSoup
import httpx
import random
from config.dir_config import LOG_DIR
import logging
import os
import datetime
import time
import json
from dao.BaseRepository import BaseRepository
baseRep = BaseRepository(collectionname="switch")

base_url = "https://www.3kns.com/"
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.DEBUG)
handler = logging.FileHandler(
    filename=os.path.join(LOG_DIR, str(datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")) + ".log"), mode="w",
    encoding="utf-8")
handler.setLevel(logging.DEBUG)
handler2 = logging.StreamHandler()
handler2.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
handler2.setFormatter(formatter)
logger.addHandler(handler)
logger.addHandler(handler2)
cookies = {
    '7YgK_2132_saltkey': 'RkG5lfz3',
    '7YgK_2132_lastvisit': '1682295230',
    '7YgK_2132_sendmail': '1',
    '7YgK_2132_ulastactivity': 'bfd9mUDF7VmSd64dQ9kBDQf3t7Agw7ee8ymBOt1Ag7pzbgXoj6ky',
    '7YgK_2132_auth': '72a6p4t59TQkwcu5I%2Fainzy9KFEqlut3K6fVcT3rwYO0YiB2iWn3YS1GrzHRx3B87bobnVVxpM%2FYA9fHAsPKwZud2A',
    '7YgK_2132_lastcheckfeed': '24724%7C1682298841',
    '7YgK_2132_member_login_status': '1',
    '7YgK_2132_noticeTitle': '1',
    '7YgK_2132_visitedfid': '2',
    '7YgK_2132_st_t': '24724%7C1682298886%7C8411a7bf421198ad41bb855a460b58aa',
    '7YgK_2132_forum_lastvisit': 'D_2_1682298886',
    '7YgK_2132_sid': 'AaZYQP',
    '7YgK_2132_lip': '149.129.103.176%2C1682298841',
    '7YgK_2132_viewid': 'tid_10885',
    '7YgK_2132_st_p': '24724%7C1682299031%7Ce54ccdf1b6bd6f776311c1a34c3e5dfb',
    '7YgK_2132_lastact': '1682299031%09home.php%09spacecp',
}

headers = {
    'authority': 'www.3kns.com',
    'accept': '*/*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    # 'cookie': '7YgK_2132_saltkey=RkG5lfz3; 7YgK_2132_lastvisit=1682295230; 7YgK_2132_sendmail=1; 7YgK_2132_ulastactivity=bfd9mUDF7VmSd64dQ9kBDQf3t7Agw7ee8ymBOt1Ag7pzbgXoj6ky; 7YgK_2132_auth=72a6p4t59TQkwcu5I%2Fainzy9KFEqlut3K6fVcT3rwYO0YiB2iWn3YS1GrzHRx3B87bobnVVxpM%2FYA9fHAsPKwZud2A; 7YgK_2132_lastcheckfeed=24724%7C1682298841; 7YgK_2132_member_login_status=1; 7YgK_2132_noticeTitle=1; 7YgK_2132_visitedfid=2; 7YgK_2132_st_t=24724%7C1682298886%7C8411a7bf421198ad41bb855a460b58aa; 7YgK_2132_forum_lastvisit=D_2_1682298886; 7YgK_2132_sid=AaZYQP; 7YgK_2132_lip=149.129.103.176%2C1682298841; 7YgK_2132_viewid=tid_10885; 7YgK_2132_st_p=24724%7C1682299031%7Ce54ccdf1b6bd6f776311c1a34c3e5dfb; 7YgK_2132_lastact=1682299031%09home.php%09spacecp',
    'referer': 'https://www.3kns.com/thread-10885-1-1.html',
    'sec-ch-ua': '"Chromium";v="112", "Microsoft Edge";v="112", "Not:A-Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.48',
    'x-requested-with': 'XMLHttpRequest',
}

params = {
    'mod': 'misc',
    'action': 'protectsort',
    'tid': '10885',
    'optionid': '12',
    'inajax': '1',
    'ajaxtarget': 'sortmessage_deanbgmm',
}

# response = requests.get('https://www.3kns.com/forum.php', params=params, cookies=cookies, headers=headers)
# logger.debug(response.text)


def get_detail(url: str):
    tid = str(url.split("-")[1])
    if not url.startswith("http"):
        url = base_url+url
    res = requests.get(url, cookies=cookies, headers=headers)
    soup = BeautifulSoup(res.text, 'html5lib')
    params["tid"] = tid
    pan_link=soup.find("div",attrs={"class":"rightbtn"}).find("a").get("href")
    logger.debug(pan_link)
    game_name=soup.find("div",attrs={"class":"title-content-title"}).text
    _, num, msg=baseRep.search_no_page({"name":game_name})
    if num>0:
        logger.info(f"{game_name}已经存在,不用插入数据库")
        
        return
    logger.debug(game_name)
    update_time=soup.find("div",attrs={"class":"title-content-time"}).text.replace("游戏更新时间: ","").strip()
    logger.debug(update_time)
    # game_sore=soup.select_one(r"div.content-wrapper-standard > div.cs-vidpf > div")[1].text.strip()
    # logger.debug(game_sore)
    pan_res = requests.get('https://www.3kns.com/forum.php',
                           params=params, cookies=cookies, headers=headers)
    pan_code=str(pan_res.text).replace(r'<?xml version="1.0" encoding="utf-8"?>',"").replace(r"<root><![CDATA[","").removesuffix(r"]]></root>").strip()

    logger.debug(pan_code)
    game = {
            "name": game_name,                
            "pan_link": pan_link,
            "pan_code": pan_code,
            "status": "need_save",
            "update_time":update_time,
        }
    logger.info("当前游戏为:"+json.dumps(game, ensure_ascii=False))            
    logger.info("插入数据库")
    baseRep.insert_one(game)
    

def get_list(page: int) -> list:
    links = []
    res = requests.get(f"https://www.3kns.com/forum-2-{page}.html",
                       cookies=cookies, headers=headers)
    soup = BeautifulSoup(res.text, 'html5lib')
    entrys = soup.find_all("article")
    for entry in entrys:
        link = entry.select_one("a")
        logger.debug(link.get("href"))
        links.append(link.get("href"))
    for link in links:
        try:
            get_detail(link)
        except Exception as e:
            logger.error(e)
            continue

        time.sleep(1)

def main():
    # 生成1-100的随机数，并调用get_list方法
    for n in range(1, 581):
        get_list(n)


if __name__ == "__main__":
    main()
    # get_list(1)
